import java.time.LocalDateTime;

public class ThredDemo1 {
    // 总任务次数
    private final static int COUNT = 10;

    public static void main(String[] args) throws InterruptedException {

        // 记录开始执行的时间
        long stime = System.currentTimeMillis();

        //多线程
        multiThead();

        // 记录结束时间
        long etime = System.currentTimeMillis();//获取当前时间的时间戳
        System.out.println("执行时间：" + (etime - stime));
    }

    /**
     * 多线程运行
     * @throws InterruptedException
     */
    private static void multiThead() throws InterruptedException {
        Thread t = new Thread(() -> {
            for (int i = 0; i < COUNT / 2; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println("新线程：执行了for循环：" + LocalDateTime.now());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

        for (int i = 0; i < COUNT / 2; i++) {
            try {
                Thread.sleep(1000);
                System.out.println("主线程：执行了for循环：" + LocalDateTime.now() );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        t.join();
    }
    /**
     * 单线程任务执行
     */
    private static void singleThread() {

        for (int i = 0; i < COUNT; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
